﻿function transport()
{
    var p = new page();
    p.c = "Way.Bus.WMS.ST.Controllers.YS.ST_PackListController";
    p.applyTo = "tabpage2";
    p.title = "运输";
    p.mainColumns = ["ROWINDEX", 'CODE', 'TOWHNO', 'STATE'];
    p.subColumns = ['THEDRIVER', 'CARNO', 'CREATEDBY', 'CREATIONDATE'];
    var where = [];
    var w1 = { ColumnName: "CODE", Value: "", Relation: "or", Symbol: "like" };
    where.push(w1);
    p.searchWhere = where;
    p.defultWhere = [{ ColumnName: "IsSubmited", Value: "1" }, {  ColumnName: "State", Value: "1", Symbol: ">" }, { ColumnName: "TheDriver", Value: webjs.userid }];
    p.fromedit = 1;
    p.cztit = "卸货作业";
    p.editindex = 0;
    p.childWhere = [{ ColumnName: "STATE", Value: "3" }];
    p.onClose = function (f) {
        if (f.watch != undefined) {
            navigator.geolocation.clearWatch(f.watch);
            f.watch = undefined;
        }
        if (f.map)
            f.map.closeInfoWindow();
    }
    p.onChildFrom = function (f, model, items) {
        $("#" + f.ffnmae + "_save > span > span.l-btn-text > span").html("卸货");
        for (var i in items) {
            var tt = items[i];
            if (tt.field == "RECEIPTCODE")
                continue;
            tt.disabled = true;
        }
        var maptab = $("#" + f.tabname).tabs('getTab', "运输业务");
        if (!maptab) {
            $("#" + f.tabname).tabs("add", {
                title: "运输业务",
                content: '<div class="easyui-panel" style="width: 100%;height: 8%;padding:2px;overflow:hidden;">'
                    + '<a href="#" id="tansport_save" class="easyui-linkbutton" style="width:50px;margin-top: 5px;"><span style="font-size:16px">运输</span></a>'
                    + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
                    + '<a href="#" id="tansport_stop" class="easyui-linkbutton" style="width:80px;margin-top: 5px;"><span style="font-size:16px">暂停运输</span></a>'
                   // + '&nbsp;&nbsp;'
                   // + '<a href="#" id="tansport_move" class="easyui-linkbutton" style="width:80px;margin-top: 5px;"><span style="font-size:16px">上一运点</span></a>'
                    + '</div><div style="width: 100%;height: 92%;" id="l-map"></div>',
                //content: '<div style="width: 100%;height: 100%;" id="l-map"></div>',
                selected: true
            });
            var map = new BMap.Map("l-map");
            map.centerAndZoom(new BMap.Point(116.404, 39.915), 14);
            map.addControl(new BMap.NavigationControl());
            map.addControl(new BMap.ScaleControl());
            f.map = map;
            $("#tansport_save").bind("click", function () {
                f.loading("启动运输中。。。");
                p.stopgps = false;
                runing(0, '',p,f);
            });
            $("#tansport_stop").bind("click", function () {
                p.stopgps = true;
            });
            $("#tansport_move").bind("click", function () {

            });
            p.onTabSelect = function (f, title, index) {
                if (title == "运输业务") {
                    var item = f.editItem;
                    if (item) {
                        // 创建地址解析器实例     
                        var myGeo = new BMap.Geocoder();
                        // 将地址解析结果显示在地图上，并调整地图视野    
                        myGeo.getPoint(item.RECEIPTADDRESS, function (point) {
                            if (point) {
                                f.map.clearOverlays();
                                f.map.centerAndZoom(point, 16);
                                var marker = new BMap.Marker(point);
                                var lab2 = new BMap.Label(item.RECEIPTADDRESS, { offset: new BMap.Size(10, -10) });
                                marker.setLabel(lab2)
                                f.map.addOverlay(marker)
                                //f.map.panTo(point);
                            }
                        }, "");
                    }
                    return;
                }
            }
        }
    }
    function runing(zt,code,p,f)
    {
        getxy(f, function (e) {
            var x = e.coords.latitude;
            var y = e.coords.longitude;
            submittransport(p, x, y, zt, code, f);
            if (zt == 0 || zt == 1) {
                var uptime = 5000;
                if (window.config && window.config.port1)
                    uptime = window.config.port1;
                setTimeout(function () {
                    if (!p.stopgps) {
                        runing(1, '', p, f);
                    }
                }, uptime);
            }
        });
    }
    function getxy(f, fn) {
        var noop = function () { };
        window.locationService.getCurrentPosition(function (pos) {
            fn(pos);
            window.locationService.stop(noop, noop)
        }, function (e) {
            fn(e);
            window.locationService.stop(noop, noop)
        });
    }
    function submittransport(p, x, y, state, code,f) {
        var c = {};
        c.id = p.datasource.selectItem.ID;
        c.x = x;
        c.y = y;
        c.zt = state;
        c.code = code;
        p.datasource.submit("ys", "ccc=" + JSON.stringify(c), function (e) {
            f.loaded();
            if (e && e.error) {
                $.messager.alert('异常', e.errormsg, 'error');
            }
            if (e.results != "")
                $.messager.alert('完成', e.results);
        });
    }
   
    p.onSave = function (f, obj) {
        runing(3, obj.RECEIPTCODE,p,f);
        f.clear();
        return false;
    }
    function zcxh(m, f, obj) {
        var packid = f.datasource.selectItem.ID;
        p.datasource.submit(m, "barcode=" + obj.RECEIPTCODE + "&packid=" + packid, function (e) {
            f.loaded();
            if (e && e.error) {
                $.messager.alert('异常', e.errormsg, 'error');
            }
            else {
                p.stopgps = true;
                runing(3, obj.RECEIPTCODE, p, f);
                $.messager.alert('消息', obj.RECEIPTCODE + '卸货完成。');
                f.clear();
            }
        });
    }
    p.init();
}